package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// DeviceActiveDuration 设备活跃详细数据
// 包括里程数及活跃时间
type DeviceActiveDuration struct {
	ID                 string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	Date               string `json:"date" gorm:"column:date;type:string;size:20;uniqueIndex:idx_date_robotID,priority:1;comment:日期"`
	RobotID            string `json:"robot_id" gorm:"column:robot_id;type:string;size:40;uniqueIndex:idx_date_robotID,priority:2;index:idx_createdAt_robotID,priority:2;comment:机器人id"`
	RunningOdometer    int64  `json:"running_odometer" gorm:"column:running_odometer;type:int;size:64;default:0;comment:运行里程"`
	RunningDuration    int64  `json:"running_duration" gorm:"column:running_duration;type:int;size:64;default:0;comment:运行时长"`
	ChargingDuration   int64  `json:"charging_duration" gorm:"column:charging_duration;type:int;size:64;default:0;comment:充电时长second"`
	TaskOdometer       int64  `json:"task_odometer" gorm:"column:task_odometer;type:int;size:64;default:-1;comment:任务里程"`
	TaskDuration       int64  `json:"task_duration" gorm:"column:task_duration;type:int;size:64;default:-1;comment:任务时长,单位ms"`
	TaskRunCount       int64  `json:"task_run_count" gorm:"column:task_run_count;type:int;size:32;default:-1;comment:任务运行次数"`
	ToTaskDuration     int64  `json:"to_task_duration" gorm:"column:to_task_duration;type:int;size:64;default:0;comment:出门时长,单位ms"`
	ToChargingDuration int64  `json:"to_charging_duration" gorm:"column:to_charging_duration;type:int;size:64;default:0;comment:回充时长,单位ms"`
	UpdatedAt          int64  `json:"updated_at" gorm:"autoUpdateTime;column:updated_at;type:int;size:64;comment:更新时间"`
	IdleDuration       int64  `json:"idle_duration" gorm:"column:idle_duration;type:int;size:64;comment:待机时长"`
	FaultDuration      int64  `json:"fault_duration" gorm:"-"` // 异常时长
	CreatedAt          int64  `json:"created_at" gorm:"autoCreateTime;column:created_at;type:int;size:64;index:idx_createdAt_robotID,priority:1;comment:创建时间"`
}

// TableName 指定表名
func (DeviceActiveDuration) TableName() string {
	return "device_active_duration"
}

// BeforeCreate 自增id
func (d *DeviceActiveDuration) BeforeCreate(tx *gorm.DB) (err error) {
	if d.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	d.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &DeviceActiveDuration{})
}
